package com.auxgroup.smarthome.controller;

import com.auxgroup.smarthome.basebean.MenuTypeEnum;
import com.auxgroup.smarthome.entity.SysMessageEntity;
import com.auxgroup.smarthome.filter.annotation.PermissionMenu;
import com.auxgroup.smarthome.service.SysMessageService;
import com.auxgroup.smarthome.vo.SysMessageIdVo;
import com.auxgroup.smarthome.vo.SysMessageTagVo;
import com.auxgroup.smarthome.vo.SysMessageVo;
import com.auxgroup.smarthome.web.ApiResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * Created by fju on 2017/8/8.
 * 站内信接口
 */
@RestController
@Api(tags = "站内消息")
public class SysMessageController {

    @Autowired
    private SysMessageService sysMessageService;

    @ApiOperation(value = "查询站内信发件列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "keyword", value = "关键字",  paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "page", value = "页码",  paramType = "query", dataType = "int",required = true),
            @ApiImplicitParam(name = "size", value = "大小",  paramType = "query", dataType = "int",required = true),
    })
    @RequestMapping(value = "/sys_messages",method = RequestMethod.GET)
    public ApiResponse<Page<SysMessageEntity>> getSysMessageList(String keyword, @RequestParam("page") Integer page, @RequestParam("size") Integer size){
        return sysMessageService.getSysMessageList(keyword,page,size);
    }

    @ApiOperation(value = "获取单条站内信",notes = "查询单条收件信的详细内容")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sysMessageId", value = "站内信Id",  paramType = "query", dataType = "string",required = true),
    })
    @RequestMapping(value = "/sys_message_received/one",method = RequestMethod.GET)
    public ApiResponse<SysMessageVo> getSysMessageList( @RequestParam("sysMessageId") String sysMessageId){
        return sysMessageService.getSysMessage(sysMessageId);
    }

    @ApiOperation(value = "新增站内信")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "title", value = "标题",  paramType = "form", dataType = "string",required = true),
            @ApiImplicitParam(name = "content", value = "内容",  paramType = "form", dataType = "string",required = true),
            @ApiImplicitParam(name = "roleId", value = "角色Id",  paramType = "form", dataType = "string",required = true),
    })
    @RequestMapping(value = "/sys_message",method = RequestMethod.POST)
    public ApiResponse<SysMessageIdVo> addSysMessage(@RequestParam("title")String title, @RequestParam("content")String content, @RequestParam("roleId")String roleId){
        return sysMessageService.addSysMessage(title,content,roleId);
    }

    @ApiOperation(value = "查询站内信收件列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "keyword", value = "关键字",  paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "page", value = "页码",  paramType = "query", dataType = "int",required = true),
            @ApiImplicitParam(name = "size", value = "大小",  paramType = "query", dataType = "int",required = true),
    })
    @RequestMapping(value = "/sys_messages_received",method = RequestMethod.GET)
    public ApiResponse<Page<SysMessageVo>> getReceivedSysMessageList(String keyword, @ApiIgnore Pageable pageable){
        return sysMessageService.getReceivedSysMessageList(keyword, pageable);
    }

    @ApiOperation(value = "获取收件站内信")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sysMessageId", value = "收件人id,可不填",  paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "progress", value = "信件状态：0.未阅 1.已阅 2.已删除，不填获取全部",  paramType = "query", dataType = "int"),
    })
    @GetMapping(value = "/in_mail/list")
    public ApiResponse<List<SysMessageVo>> getReceivedSysMessageList(String sysMessageId, Integer progress){
        return sysMessageService.getSysMessageListByProgress(sysMessageId, progress);
    }

    @ApiOperation(value = "标记站内信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sysMessageId", value = "站内信Id,多个用 , 隔开",  paramType = "form", dataType = "string",required = true),
            @ApiImplicitParam(name = "progress", value = "进度：0.未阅 1.已阅 2.已删除",  paramType = "form", dataType = "int",required = true),
    })
    @RequestMapping(value = "/sys_message_received",method = RequestMethod.PUT)
    @PermissionMenu(id= "umps_inMailModule_view list_mark", parentId = "umps_inMailModule_view list", name = "标记站内信", menuType = MenuTypeEnum.BUTTON)
    public ApiResponse<SysMessageTagVo> tagSysMessage(@RequestParam("sysMessageId")String sysMessageId, @RequestParam("progress")Integer progress){
        return sysMessageService.tagSysMessage(sysMessageId,progress);
    }

    @ApiOperation(value = "清空站内信收件箱")
    @RequestMapping(value = "/sys_message_received",method = RequestMethod.DELETE)
    @PermissionMenu(id= "umps_inMailModule_view_clear_received", parentId = "umps_inMailModule_view list", name = "清空站内信收件箱", menuType = MenuTypeEnum.BUTTON)
    public ApiResponse emptySysMessage(){
        return sysMessageService.clearAllReceiveInMailOfCurrUser();
    }

    @ApiOperation(value = "清空站内信发件箱")
    @RequestMapping(value = "/in_mail/clear_all_mail",method = RequestMethod.DELETE)
    @PermissionMenu(id= "umps_inMailModule_view_clear_send", parentId = "umps_inMailModule_view list", name = "清空站内信发件箱", menuType = MenuTypeEnum.BUTTON)
    public ApiResponse clearAllReceiveInMailOfCurrUser(){
        return sysMessageService.clearAllSendInMailOfCurrUser();
    }

    @ApiOperation(value = "删除站内信发件箱")
    @DeleteMapping(value = "/in_mail/delete_mail")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sysMessageIds", value = "站内信Id,多个用 , 隔开",  paramType = "query", dataType = "string",required = true),
    })
    @PermissionMenu(id= "umps_inMailModule_view_delete_send_mail", parentId = "umps_inMailModule_view list", name = "删除站内信发件箱", menuType = MenuTypeEnum.BUTTON)
    public ApiResponse deleteSysMessage(String sysMessageIds){
        return sysMessageService.deleteSendInMail(sysMessageIds);
    }

}
